package simu

import (
	"fmt"
	"topobuild/utils"

	"github.com/xuri/excelize/v2"
)

/*
rsing_id	dev_id	ifindex	display_name	ifspeed	ip_address	ipv6_address
1	1	1	eth1	10000000000	200.10.0.1/24	2001:DB8::4178
1	1	2	eth2	10000000000	200.10.0.5/24	2001:DB8::4179
1	2	1	eth1	10000000000	200.10.0.2/24	2001:DB8::4180
1	2	2	eth2	10000000000	200.10.0.9/24	2001:DB8::4181
1	3	1	eth1	10000000000	200.10.0.6/24	2001:DB8::4182
1	3	2	eth2	10000000000	200.10.0.10/24	2001:DB8::4183
*/

type TableIntface struct {
	RsingId     int    `json:"rsing_id"`
	DevId       int    `json:"dev_id"`
	IfIndex     int    `json:"ifindex"`
	DisplayName string `json:"display_name"`
	IfSpeed     int    `json:"ifspeed"`
	IpAddress   string `json:"ip_address"`
	Ipv6Address string `json:"ipv6_address"`
}

// 将TableIntface中的数据,写入到指定xlsx文件的interface表中
func WriteIntfaceXlsx(filename string, intfs []TableIntface) {
	// 如果filename已经存在,则直接打开,否则创建一个新文件
	utils.CheckFile(filename)
	xlsx, err := excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer xlsx.Close()
	// Create a new sheet.
	index, err := xlsx.NewSheet("interface")
	if err != nil {
		fmt.Println(err)
		return
	}
	// Set value of a cell.
	xlsx.SetCellValue("interface", "A1", "rsing_id")
	xlsx.SetCellValue("interface", "B1", "dev_id")
	xlsx.SetCellValue("interface", "C1", "ifindex")
	xlsx.SetCellValue("interface", "D1", "display_name")
	xlsx.SetCellValue("interface", "E1", "ifspeed")
	xlsx.SetCellValue("interface", "F1", "ip_address")
	xlsx.SetCellValue("interface", "G1", "ipv6_address")
	// 设置单元格样式
	// style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center"}}`)
	// xlsx.SetCellStyle("interface", "A1", "G1", style)
	// 设置列宽
	// xlsx.SetColWidth("interface", "A", "G", 20)
	// 设置行高
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)
	// Save xlsx file by the given path.
	for i, intf := range intfs {
		xlsx.SetCellValue("interface", fmt.Sprintf("A%d", i+2), intf.RsingId)
		xlsx.SetCellValue("interface", fmt.Sprintf("B%d", i+2), intf.DevId)
		xlsx.SetCellValue("interface", fmt.Sprintf("C%d", i+2), intf.IfIndex)
		xlsx.SetCellValue("interface", fmt.Sprintf("D%d", i+2), intf.DisplayName)
		xlsx.SetCellValue("interface", fmt.Sprintf("E%d", i+2), intf.IfSpeed)
		xlsx.SetCellValue("interface", fmt.Sprintf("F%d", i+2), intf.IpAddress)
		xlsx.SetCellValue("interface", fmt.Sprintf("G%d", i+2), intf.Ipv6Address)
	}
	if err := xlsx.SaveAs(filename); err != nil {
		fmt.Println(err)
	}
}
